package router

import (
	"github.com/gin-gonic/gin"
	"myBlog/utils"
	"os"
	"path"
)

// path: 路径名 比如 article/网络编程 需要注意的是只有最后一个文件夹不存在才会创建---如果大于两个需要自己创建
// fileName: 可选--自定义文件名
// file: 需要上传的文件
func uploadImage(c *gin.Context) {
	uploadPath := c.PostForm("path")
	file, err := c.FormFile("file")
	uploadFileName := c.PostForm("fileName")
	if len(uploadFileName) == 0 {
		uploadFileName = file.Filename
	}
	pwd, _ := os.Getwd()
	temPath := path.Join(pwd+"\\img", uploadPath)
	nPath := utils.DealPath(temPath)
	_ = utils.AddFolderFromPath(nPath)
	filePath := utils.DealPath(path.Join(nPath, uploadFileName))
	err = c.SaveUploadedFile(file, filePath)
	if err != nil {
		println(err.Error())
		c.JSON(500, gin.H{
			"msg": "上传文件失败",
		})
		return
	}
	c.JSON(200, gin.H{})
}

func delImg(c *gin.Context) {

}

//图片管理--作为一个图床
func InitImage(r *gin.Engine) {
	g := r.Group("/image")
	g.POST("/uploadImage", uploadImage)
	g.POST("/delImg", delImg)
}
